<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="newList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="杆号序号" prop="ganhaoid" />
      <el-table-column label="杆号" prop="ganhaoname" />
      <el-table-column label="工区ID" prop="gq_id" />
      <el-table-column label="工区名称" prop="gqname" />
      <el-table-column label="战区ID" prop="zhanqu_id" />
      <el-table-column label="战区名称" prop="zaname" />
      <el-table-column label="隧道编号" prop="sd_id" />
      <el-table-column label="隧道名称" prop="sd_name" />
      <el-table-column label="支柱型号" prop="zztype" />
      <el-table-column label="负责人" prop="directors" />
      <el-table-column label="公里标" prop="gonglibiao" />
      <el-table-column label="跨距" prop="kuaju" />
      <el-table-column label="概述" prop="overview" />
      <el-table-column label="备注" prop="remark" />

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button size="mini" type="text" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      :title="dialogTitle"
      :visible.sync="editDialogVisible"
      width="30%"
      @close="handleDialogClose"
    >
      <el-form :model="addForm">
        <el-row>
          <el-col :span="12">
            <el-form-item label="杆号序号" label-width="100px">
              <el-input
                v-model="addForm.ganhaoid"
                autocomplete="off"
              ></el-input>
            </el-form-item>
            <el-form-item label="杆号" label-width="100px">
              <el-input
                v-model="addForm.ganhaoname"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工区ID" label-width="100px">
              <el-input v-model="addForm.gq_id" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="工区名称" label-width="100px">
              <el-input v-model="addForm.gqname" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="站区ID" label-width="100px">
              <el-input
                v-model="addForm.zhanqu_id"
                autocomplete="off"
              ></el-input>
            </el-form-item>
            <el-form-item label="站区名称" label-width="100px">
              <el-input v-model="addForm.zaname" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="隧道编号" label-width="100px">
              <el-input v-model="addForm.sd_id" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="隧道名称" label-width="100px">
              <el-input v-model="addForm.sd_name" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="支柱型号" label-width="100px">
              <el-input v-model="addForm.zztype" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="负责人" label-width="100px">
              <el-input
                v-model="addForm.directors"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公里标" label-width="100px">
              <el-input
                v-model="addForm.gonglibiao"
                autocomplete="off"
              ></el-input>
            </el-form-item>
            <el-form-item label="跨距" label-width="100px">
              <el-input v-model="addForm.kuaju" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="概述" label-width="100px">
          <el-input
            type="textarea"
            v-model="addForm.overview"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="备注" label-width="100px">
          <el-input
            type="textarea"
            v-model="addForm.remark"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取消</el-button>
        <el-button
          type="primary"
          @click="
            currentOperation === 'add' ? handleAddConfirm : handleEditConfirm
          "
          >确认</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { selectAll } from "@/api/gyjc/new";
export default {
  name: "GanhaoList",
  data() {
    return {
      loading: true,
      newList: [],
      editDialogVisible: false,
      addForm: {
        ganhaoid: null,
        ganhaoname: "",
        gq_id: null,
        gqname: "",
        zhanqu_id: null,
        zaname: "",
        sd_id: null,
        sd_name: "",
        zztype: "",
        directors: "",
        gonglibiao: "",
        kuaju: "",
        overview: "",
        remark: "",
      },
      editDialogVisible: false,
      currentOperation: "",
      dialogTitle: "新增杆号",
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      selectAll()
        .then((response) => {
          console.log("Full Response:", response); 
          if (response && response.data) {
            console.log("Response Data:", response.data); 
            this.newList = response.data;
          } else {
            this.newList = [];
          }
          this.loading = false;
          console.log(this.newList);
        })
        .catch((error) => {
          console.error("接口请求出错:", error);
          this.loading = false;
        });
    },
    handleAdd() {
      // 打开新增对话框
      this.editDialogVisible = true;
      this.dialogTitle = "新增杆号";
      this.addForm = {
        ganhaoid: null,
        ganhaoname: "",
        gq_id: null,
        gqname: "",
        zhanqu_id: null,
        zaname: "",
        sd_id: null,
        sd_name: "",
        zztype: "",
        directors: "",
        gonglibiao: "",
        kuaju: "",
        overview: "",
        remark: "",
      };

      this.currentOperation = "add";
    },
    handleAddConfirm() {
      if (this.currentOperation === "add") {
        request({
          url: `/new/add`,
          method: "post",
          data: this.addForm,
        })
          .then(() => {
            this.$message({
              type: "success",
              message: "添加成功!",
            });
            // 关闭对话框
            this.editDialogVisible = false;
            // 重新加载数据
            this.fetchData();
          })
          .catch((error) => {
            console.error("添加失败:", error);
            this.$message({
              type: "error",
              message: "添加失败",
            });
          });
      }
    },
    handleUpdate(row) {
      this.editDialogVisible = true;

      this.dialogTitle = "编辑杆号";

      this.addForm = { ...row };

      this.currentOperation = "edit";
    },

    handleEditConfirm() {
      // 确认编辑记录
      if (this.currentOperation === "edit") {
        request({
          url: `/new/update/${this.addForm.ganhaoid}`, // 根据实际接口调整
          method: "put",
          data: this.addForm,
        })
          .then(() => {
            this.$message({
              type: "success",
              message: "更新成功!",
            });
            // 关闭对话框
            this.editDialogVisible = false;
            // 重新加载数据
            this.fetchData();
          })
          .catch((error) => {
            console.error("更新失败:", error);
            this.$message({
              type: "error",
              message: "更新失败",
            });
          });
      }
    },
    handleDelete(row) {
      // 确认删除操作
      this.$confirm("确认删除该记录吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          request({
            url: `/new/delete/${row.ganhaoid}`,
            method: "delete",
          })
            .then(() => {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              // 重新加载数据
              this.fetchData();
            })
            .catch((error) => {
              console.error("删除失败:", error);
              this.$message({
                type: "error",
                message: "删除失败",
              });
            });
        })
        .catch(() => {
          // 取消删除操作
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleSelectionChange(val) {
      // 你的处理逻辑
      console.log(val);
    },
    handleDialogClose() {
      // 你的处理逻辑
      this.editDialogVisible = false;
    },
  },
};
</script>

<style scoped>
.app-container {
  margin: 20px;
}

.mb8 {
  margin-bottom: 8px;
}

.dialog-footer {
  text-align: right;
}
</style>